package net.sunzc.takephoto;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import net.sunzc.hwfacetest.R;
import net.sunzc.takephoto.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/11.
 */
public class PhotoActivity<P extends PhotoItem> extends Activity implements AdapterView.OnItemClickListener {
    private static final String EXTRA_IMAGE_LIST = "imagelist";
    private PhotoAdapter mPhotoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void setContentView(int layoutResID) {
        View rootView = getLayoutInflater().inflate(R.layout.activity_photo, null);
        FrameLayout container = (FrameLayout) rootView.findViewById(R.id.container);
        View childView = getLayoutInflater().inflate(layoutResID, null);
        container.addView(childView);
        super.setContentView(rootView);
        GridView photoView = (GridView) findViewById(R.id.image_grid);
        mPhotoAdapter = new PhotoAdapter<P>(this);
        if (getIntent() != null) {
            mPhotoAdapter.addData((List<PhotoItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST));
        }
        photoView.setAdapter(mPhotoAdapter);
        photoView.setOnItemClickListener(this);
    }

    public PhotoAdapter getAdapter() {
        return mPhotoAdapter;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    public static class PhotoAdapter<T extends PhotoItem> extends BaseAdapter {
        private final Context mContext;
        private ArrayList<T> mPhotos = new ArrayList<>();

        PhotoAdapter(Context context) {
            this.mContext = context;
        }

        public ArrayList<T> getPhotos() {
            return mPhotos;
        }

        @Override
        public int getCount() {
            return mPhotos.size();
        }


        @Override
        public T getItem(int i) {
            return mPhotos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.photo_item, null);
                view.setTag(new PhotoItemHolder(view));
            }
            PhotoItemHolder item = (PhotoItemHolder) view.getTag();
            T photoItem = getItem(i);
            item.imageView.setImageBitmap(photoItem.getImage());
            if (!TextUtils.isEmpty(photoItem.getLabel())) {
                item.textView.setText(photoItem.getLabel());
            }
            return view;
        }

        void addData(List<T> photoItems) {
            if (StringUtils.isNull(photoItems)) {
                return;
            }
            mPhotos.clear();
            mPhotos.addAll(photoItems);
            notifyDataSetChanged();
        }

        public void add(T photoItem) {
            mPhotos.add(photoItem);
            notifyDataSetChanged();
        }

        public static class PhotoItemHolder {
            ImageView imageView;
            TextView textView;

            PhotoItemHolder(View view) {
                imageView = (ImageView) view.findViewById(R.id.image);
                textView = (TextView) view.findViewById(R.id.label);
            }
        }
    }
}
